package com.cn.kehong.service;


import com.cn.kehong.api.*;
import com.cn.kehong.conf.Config;
import com.cn.kehong.dao.*;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.*;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import freemarker.template.utility.DateUtil;
import jxl.write.DateTime;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@EnableScheduling
@Service
public class LineTaskServiceImpl implements ILineTaskService {
    @Autowired
    private LineTaskMapper lineTaskMapper;
    @Autowired
    private ProductChuciPeriodMapper productChuciPeriodMapper;
    @Autowired
    private Config config;
    @Autowired
    private ProductPeiTouMapper productPeiTouMapper;

    @Autowired
    private IProductThickPeriodService productThickPeriodService;

    @Autowired
    private IProductThinPeriodService productThinPeriodService;

    @Autowired
    private IProductChuciPeriodService productChuciPeriodService;
    @Autowired
    private ProductPackageJobMapper productPackageJobMapper;
    @Autowired
    private ProductPackageMapper productPackageMapper;
    @Autowired
    private XunjianTaskMapper xunjianTaskMapper;
    @Autowired
    private FileInfoMapper fileInfoMapper;

    @Autowired
    private IProductGzPeriodService productGzPeriodService;

    @Override
    public List<LineTask> getLineTaskList(QueryForm queryForm) throws Exception {
        queryForm.setNameStr(getNameStr(queryForm.getType()));
//        queryForm.setTypeList(getTypeStr(queryForm.getType()));
        int count = lineTaskMapper.getLineTaskCount(queryForm);
        if (count == 0) {
            return Lists.newArrayList();
        }
        List<LineTask> lineTasks = lineTaskMapper.getLineTaskList(queryForm);
        for (LineTask lineTask : lineTasks) {
            if (StringUtils.isNotBlank(lineTask.getPhotoId())) {
                String[] split = lineTask.getPhotoId().split(",");
                List<String> fileUrlList = new ArrayList<>();
                for (String s : split) {
                    FileInfoExample example = new FileInfoExample();
                    example.createCriteria().andIdEqualTo(s).andStatusEqualTo(Constants.NORMAL_STATUS);
                    List<FileInfo> infos = fileInfoMapper.selectByExample(example);
                    if (!infos.isEmpty()) {
                        String fileUrl = infos.get(0).getFileUrl();
                        fileUrlList.add(fileUrl);
                    }
                }
                String[] fileUrlsArray = fileUrlList.toArray(new String[0]);
                lineTask.setFileUrls(fileUrlsArray);
            }
        }
        if (lineTasks.size() > 0) {
            lineTasks.get(0).setCount(count);
        }
        return lineTasks;
    }

    @Override
    public List<LineTask> getLineTaskListByTobe(QueryForm queryForm) throws Exception {
        queryForm.setNameStr(getNameStr(queryForm.getType()));
        ArrayList<String> list = new ArrayList<>();
        list.add("1");
        queryForm.setTypeList(list);
        int count = lineTaskMapper.getLineTaskCount(queryForm);
        if (count == 0) {
            return Lists.newArrayList();
        }
        List<LineTask> lineTasks = lineTaskMapper.getLineTaskList(queryForm);
        for (LineTask lineTask : lineTasks) {
            if (StringUtils.isNotBlank(lineTask.getPhotoId())) {
                String[] split = lineTask.getPhotoId().split(",");
                List<String> fileUrlList = new ArrayList<>();
                for (String s : split) {
                    FileInfoExample example = new FileInfoExample();
                    example.createCriteria().andIdEqualTo(s).andStatusEqualTo(Constants.NORMAL_STATUS);
                    List<FileInfo> infos = fileInfoMapper.selectByExample(example);
                    if (!infos.isEmpty()) {
                        String fileUrl = infos.get(0).getFileUrl();
                        fileUrlList.add(fileUrl);
                    }
                }
                String[] fileUrlsArray = fileUrlList.toArray(new String[0]);
                lineTask.setFileUrls(fileUrlsArray);
            }
        }
        if (lineTasks.size() > 0) {
            lineTasks.get(0).setCount(count);
        }
        return lineTasks;
    }

    @Override
    public String updateLineTask1(LineTask lineTask) throws Exception {
        String id = lineTask.getId();
        if (StringUtils.isEmpty(id)) {
            return "id is null";
        }
        LineTaskExample example = new LineTaskExample();
        example.createCriteria().andIdEqualTo(id);
        lineTask.setUpdateTime(new Date());
        ArrayList<String> list = new ArrayList<>();
        list.add("新滤网上线首检");
//        list.add("振筛清理");
//        list.add("干燥筛网检查");
        list.add("混料振筛检查清理");
//        list.add("小推车和磁棒使用");
//        list.add("干燥U型管检查");
//        list.add("清洗罐清水流转");
//        list.add("砂磨机清洗");
//        list.add("混料筛网检查");
//        list.add("干燥送风机清理");
        if (list.contains(lineTask.getTaskName())) {
            if (StringUtils.isNotEmpty(lineTask.getBanZhang()) && StringUtils.isNotEmpty(lineTask.getPinKongUser())) {
                lineTask.setLineStatus("2");
            }
        } else {
            lineTask.setLineStatus("2");
        }
        int count = lineTaskMapper.updateByExampleSelective(lineTask, example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateLineTaskByBz(LineTask lineTask) throws Exception {
        String id = lineTask.getId();
        if (StringUtils.isEmpty(id)) {
            return "id is null";
        }
        LineTaskExample example = new LineTaskExample();
        example.createCriteria().andIdEqualTo(id);
        lineTask.setUpdateTime(new Date());
        if (lineTask.getTaskName().equals("包装机维保")) {
            lineTask.setLineStatus("2");
        }
        int count = lineTaskMapper.updateByExampleSelective(lineTask, example);
        return CommonUtil.outStr(count);
    }


    @Override
    public Map<String, Integer> getLineTaskToday() throws Exception {
        LocalDateTime startDateTime = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endDateTime = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startTime = startDateTime.format(formatter);
        String endTime = endDateTime.format(formatter);
        Map<String, Integer> taskCounts = new HashMap<>();
        QueryForm queryForm = new QueryForm();
        queryForm.setStatus("0");
        queryForm.setStartTime(startTime);
        queryForm.setEndTime(endTime);
        String[] taskNames = {"新滤网上线首检", "投料除磁设备清理", "混料除磁器清洗", "振筛清理", "过滤器筛网检查", "磨砂机料仓压力", "干燥送风机清理", "干燥U型管检查", "干燥筛网检查", "包装机维保"};
        for (String taskName : taskNames) {
            queryForm.setTaskName(taskName);
            int count = lineTaskMapper.getcountByToday(queryForm);
            taskCounts.put(taskName, count);
        }
        return taskCounts;
    }

    @Override
    public String updateLineTaskByWc(LineTask lineTask) throws Exception {
        String id = lineTask.getId();
        if (StringUtils.isEmpty(id)) {
            return "id is null";
        }

        LineTaskExample example = new LineTaskExample();
        example.createCriteria().andIdEqualTo(id).andStatusNotEqualTo(Constants.DEL_STATUS);
        int count = lineTaskMapper.updateByExampleSelective(lineTask, example);
        return CommonUtil.outStr(count);
    }

    @Override
    public Map<String, Object> getPkListByKb(QueryForm queryForm) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        //品控待确认待完成
        List<LineTask> CompletedList = lineTaskMapper.getProductionCompletedByPk(queryForm);
        List<LineTask> filteredList = CompletedList.stream()
                .filter(lineTask -> !lineTask.getTaskName().equals("包装机维保"))
                .collect(Collectors.toList());
        for (LineTask lineTask : filteredList) {
            String lineStatus = lineTask.getLineStatus();
            switch (lineStatus) {
                case "0":
                    lineTask.setPcStatus("待处理");
                    break;
                case "1":
                    lineTask.setPcStatus("待确认");
                    break;
                case "2":
                    lineTask.setPcStatus("已完成");
                    break;
            }
        }
        resultMap.put("CompletedList", filteredList);
        resultMap.put("CompletedListCount", filteredList.size());

        //品控待巡检
        XunjianTaskExample example = new XunjianTaskExample();
        example.createCriteria().andXjStatusEqualTo("0").andStatusEqualTo(Constants.NORMAL_STATUS);
        List<XunjianTask> xunjianTasks = xunjianTaskMapper.selectByExample(example);
        for (XunjianTask xunjianTask : xunjianTasks) {
            String xjStatus = xunjianTask.getXjStatus();
            switch (xjStatus) {
                case "0":
                    xunjianTask.setPcStatus("待处理");
                    break;
                case "1":
                    xunjianTask.setPcStatus("待确认");
                    break;
                case "2":
                    xunjianTask.setPcStatus("已完成");
                    break;
            }
        }
        resultMap.put("xunjianTasks", xunjianTasks);
        resultMap.put("xunjianTasksCount", xunjianTasks.size());
        return resultMap;
    }

    @Override
    public String lineTaskCancel(String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            return "主键不能为空";
        }
        LineTask lineTask = new LineTask();
        lineTask.setUpdateTime(new Date());
        lineTask.setLineStatus("4");
        LineTaskExample example = new LineTaskExample();
        example.createCriteria().andIdEqualTo(id);
        int count = lineTaskMapper.updateByExampleSelective(lineTask, example);
        if (count < 1) {
            return "取消失败！";
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public Map<String, Object> getLineTaskByNewCount(QueryForm queryForm) throws Exception {
        //线边任务
        Map<String, Object> resultMap = new HashMap<>();
        ArrayList<String> MaList = new ArrayList<>();
        MaList.add("新滤网上线首检");
        MaList.add("投料除磁设备清理");
        MaList.add("混料除磁器清洗");
        MaList.add("混料振筛检查清理");
        MaList.add("混料筛网检查");
        MaList.add("粗磨记录");
        MaList.add("细磨记录");
        MaList.add("砂磨机清洗");
        MaList.add("小推车和磁棒使用");
        MaList.add("清洗罐清水流转");
        MaList.add("砂磨机料仓压力");

        ArrayList<String> MbList = new ArrayList<>();
        MbList.add("送风机清理");
        MbList.add("U型管检查");
        MbList.add("干燥筛网检查");
        MbList.add("干燥送风机清理");
        MbList.add("干燥U型管检查");

        ArrayList<String> McList = new ArrayList<>();
        McList.add("干燥工序记录");

        ArrayList<String> MdList = new ArrayList<>();
        MdList.add("过滤器筛网检査");
        MdList.add("包装机维保");

        if (queryForm.getType().equals("1")) {

            LineTaskExample example = new LineTaskExample();
            example.createCriteria().andTaskNameIn(MaList).andLineStatusEqualTo("0");
            int MaCount = lineTaskMapper.countByExample(example);
            resultMap.put("MaCount", MaCount);

            LineTaskExample example1 = new LineTaskExample();
            example1.createCriteria().andTaskNameIn(MbList).andLineStatusEqualTo("0");
            int MbCount = lineTaskMapper.countByExample(example1);
            resultMap.put("MbCount", MbCount);

            LineTaskExample example2 = new LineTaskExample();
            example2.createCriteria().andTaskNameIn(McList).andLineStatusEqualTo("0");
            int McCount = lineTaskMapper.countByExample(example2);
            resultMap.put("McCount", McCount);

            LineTaskExample example3 = new LineTaskExample();
            example3.createCriteria().andTaskNameIn(MdList).andLineStatusEqualTo("0");
            int MdCount = lineTaskMapper.countByExample(example3);
            resultMap.put("MdCount", MdCount);

        } else {
            //线边任务待确认
            LineTaskExample example = new LineTaskExample();
            example.createCriteria().andTaskNameIn(MaList).andLineStatusEqualTo("1");
            int MaCount = lineTaskMapper.countByExample(example);
            resultMap.put("MaCount", MaCount);

            LineTaskExample example1 = new LineTaskExample();
            example1.createCriteria().andTaskNameIn(MbList).andLineStatusEqualTo("1");
            int MbCount = lineTaskMapper.countByExample(example1);
            resultMap.put("MbCount", MbCount);

            LineTaskExample example2 = new LineTaskExample();
            example2.createCriteria().andTaskNameIn(McList).andLineStatusEqualTo("1");
            int McCount = lineTaskMapper.countByExample(example2);
            resultMap.put("McCount", McCount);

            LineTaskExample example3 = new LineTaskExample();
            example3.createCriteria().andTaskNameIn(MdList).andLineStatusEqualTo("1");
            int MdCount = lineTaskMapper.countByExample(example3);
            resultMap.put("MdCount", MdCount);
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> getLineTaskByNewType(QueryForm queryForm) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        ArrayList<String> list = new ArrayList<>();
        if (queryForm.getType().equals("MA")) {
            String[] types = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
            String[] names = {"xlw", "tlcc", "hlccq", "hlzs", "swjc", "cmjl", "xmjl", "msj", "xtc", "qxg", "msjlc"};
            for (int i = 0; i < types.length; i++) {
                queryForm.setType(types[i]);
                queryForm.setNameStr(getNameStr(queryForm.getType()));
                //待处理
                list.add("0");
                queryForm.setTypeList(list);
                int count = lineTaskMapper.getLineTaskCount(queryForm);
                resultMap.put(names[i] + "Count", count);
            }
        } else if (queryForm.getType().equals("MB")) {
            String[] types = {"11", "12", "13", "14", "15"};
            String[] names = {"sfj", "uxg", "gzsw", "gzsfj", "gzuxg"};
            for (int i = 0; i < types.length; i++) {
                queryForm.setType(types[i]);
                queryForm.setNameStr(getNameStr(queryForm.getType()));
                //待处理
                list.add("0");
                queryForm.setTypeList(list);
                int count = lineTaskMapper.getLineTaskCount(queryForm);
                resultMap.put(names[i] + "Count", count);
            }
        } else if (queryForm.getType().equals("MC")) {
            String[] types = {"16"};
            String[] names = {"gzgx"};
            for (int i = 0; i < types.length; i++) {
                queryForm.setType(types[i]);
                queryForm.setNameStr(getNameStr(queryForm.getType()));
                //待处理
                list.add("0");
                queryForm.setTypeList(list);
                int count = lineTaskMapper.getLineTaskCount(queryForm);
                resultMap.put(names[i] + "Count", count);
            }
        } else if (queryForm.getType().equals("MD")) {
            String[] types = {"17", "18"};
            String[] names = {"glq", "bzj"};
            for (int i = 0; i < types.length; i++) {
                queryForm.setType(types[i]);
                queryForm.setNameStr(getNameStr(queryForm.getType()));
                //待处理
                list.add("0");
                queryForm.setTypeList(list);
                int count = lineTaskMapper.getLineTaskCount(queryForm);
                resultMap.put(names[i] + "Count", count);
            }
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> getLineTaskByNewTypeQr(QueryForm queryForm) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        ArrayList<String> list = new ArrayList<>();
        list.add("1");
        if (queryForm.getType().equals("MA")) {
            String[] types = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
            String[] names = {"xlw", "tlcc", "hlccq", "hlzs", "swjc", "cmjl", "xmjl", "msj", "xtc", "qxg", "msjlc"};
            for (int i = 0; i < types.length; i++) {
                queryForm.setTypeList(list);
                queryForm.setType(types[i]);
                queryForm.setNameStr(getNameStr(queryForm.getType()));
                int count = lineTaskMapper.getLineTaskCount(queryForm);
                resultMap.put(names[i] + "Count", count);
            }
        } else if (queryForm.getType().equals("MB")) {
            String[] types = {"11", "12", "13", "14", "15"};
            String[] names = {"sfj", "uxg", "gzsw", "gzsfj", "gzuxg"};
            for (int i = 0; i < types.length; i++) {
                queryForm.setTypeList(list);
                queryForm.setType(types[i]);
                queryForm.setNameStr(getNameStr(queryForm.getType()));
                int count = lineTaskMapper.getLineTaskCount(queryForm);
                resultMap.put(names[i] + "Count", count);
            }
        } else if (queryForm.getType().equals("MC")) {
            String[] types = {"16"};
            String[] names = {"gzgx"};
            for (int i = 0; i < types.length; i++) {
                queryForm.setTypeList(list);
                queryForm.setType(types[i]);
                queryForm.setNameStr(getNameStr(queryForm.getType()));
                int count = lineTaskMapper.getLineTaskCount(queryForm);
                resultMap.put(names[i] + "Count", count);
            }
        } else if (queryForm.getType().equals("MD")) {
            String[] types = {"17", "18"};
            String[] names = {"glq", "bzj"};
            for (int i = 0; i < types.length; i++) {
                queryForm.setTypeList(list);
                queryForm.setType(types[i]);
                queryForm.setNameStr(getNameStr(queryForm.getType()));
                int count = lineTaskMapper.getLineTaskCount(queryForm);
                resultMap.put(names[i] + "Count", count);
            }
        }
        return resultMap;
    }

    @Override
    public void exportLineTask(QueryForm queryForm, HttpServletResponse response) throws Exception {
        Map<String, Object> result = Maps.newHashMap();
        queryForm.setNameStr(getNameStr(queryForm.getType()));
        queryForm.setTypeList(getTypeStr(queryForm.getType()));
        List<LineTask> lineTasks = lineTaskMapper.getLineTaskList(queryForm);
        result.put("lineTasks", lineTasks);
        result.put("title", queryForm.getNameStr());
        String fileName = CommonUtil.generateRandomNum("品控-") + ".xlsx";
        String filePath = config.filePath + fileName;
        String nameStr = queryForm.getNameStr() + ".xlsx";
        if (queryForm.getType().equals("0")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "type.xlsx", response);
        }
//        else if (queryForm.getType().equals("1")) {
//            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "type1.xlsx", response);
//        }
        //投料除磁设备清理
        else if (queryForm.getType().equals("1")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "type2.xlsx", response);
        }//混料除磁器清洗
        else if (queryForm.getType().equals("2")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "type3.xlsx", response);
        }//混料振筛检查清理
        else if (queryForm.getType().equals("3")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "type4.xlsx", response);
        }//混料筛网检查
        else if (queryForm.getType().equals("4") || queryForm.getType().equals("18")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "type5.xlsx", response);
        }//送风机清理、干燥送风机清理
        else if (queryForm.getType().equals("11") || queryForm.getType().equals("14")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "type6.xlsx", response);
        }// U型管检查、干燥U型管检查
        else if (queryForm.getType().equals("12") || queryForm.getType().equals("15")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "type7.xlsx", response);
        }//包装机维保
        else if (queryForm.getType().equals("18")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "type8.xlsx", response);
        }//干燥筛网检査
        else if (queryForm.getType().equals("13")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "type9.xlsx", response);
        }
        //磨砂机料仓压力
        else if (queryForm.getType().equals("10")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "types1.xlsx", response);
        }
        //粗磨记录
        else if (queryForm.getType().equals("5")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "cmType.xlsx", response);
        }
        //细磨记录
        else if (queryForm.getType().equals("6")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "xmType.xlsx", response);
        }
        //砂磨机清洗
        else if (queryForm.getType().equals("7")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "smjqxType.xlsx", response);
        }
        //小推车和磁棒使用
        else if (queryForm.getType().equals("8")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "xtcType.xlsx", response);
        }
        //清洗罐清水流转
        else if (queryForm.getType().equals("9")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "qxgType.xlsx", response);
        }
        //干燥工序记录
        else if (queryForm.getType().equals("16")) {
            TemplateExcelUtils.downLoadExcel(filePath, nameStr, result, "gzgxType.xlsx", response);
        }
    }

//  @Override
//  public List<LineTask> getLineTaskListByApp(QueryForm queryForm) throws Exception {
//    int count = lineTaskMapper.getLineTaskListCountByApp(queryForm);
//    if(count == 0){
//      return Lists.newArrayList();
//    }
//    List<LineTask> list = lineTaskMapper.getLineTaskListByApp(queryForm);
//    list.get(0).setCount(count);
//    return list;
//  }

    @Override
    public Map<String, Object> getLineTaskCountByApp(QueryForm queryForm) throws Exception {
        //配料数量
        ProductPeiTouExample examples = new ProductPeiTouExample();
        ProductPeiTouExample.Criteria criteria = examples.createCriteria();
        criteria.andStatusEqualTo(Constants.NORMAL_STATUS).andPeiStatusEqualTo("0").andFactoryNameEqualTo(queryForm.getFactoryName());
        int pl = productPeiTouMapper.countByExample(examples);

        Map<String, Object> resultMap = new HashMap<>();
        LineTaskExample example = new LineTaskExample();
        example.createCriteria().andLineTypeEqualTo("1").andLineStatusEqualTo("0").andFactoryNameEqualTo(queryForm.getFactoryName());
        //批次
        int pc = lineTaskMapper.countByExample(example);
        //本班
        LineTaskExample example1 = new LineTaskExample();
        example1.createCriteria().andLineTypeEqualTo("2").andLineStatusEqualTo("0").andFactoryNameEqualTo(queryForm.getFactoryName());
        int bb = lineTaskMapper.countByExample(example1);
        //今日或者其他
        int qt = lineTaskMapper.countByTodayOrqt(queryForm.getFactoryName());
        //主管待确认
        int zg = lineTaskMapper.getCountByZg(queryForm.getFactoryName());
        //品控待确认
//        LineTaskExample taskExample2 = new LineTaskExample();
//        taskExample2.createCriteria().andLineStatusEqualTo("1").andTaskNameNotEqualTo("包装机维保");
//        int pk = lineTaskMapper.countByExample(taskExample2);
        int pk = lineTaskMapper.getProductionCompletedByPkCount(queryForm.getFactoryName());

        resultMap.put("pc", pc);
        resultMap.put("bb", bb);
        resultMap.put("qt", qt);
        resultMap.put("zg", zg);
        resultMap.put("pk", pk);
        resultMap.put("pl", pl);
        return resultMap;
    }

    @Override
    public List<ProductChuciPeriod> getChuciProductCode(QueryForm queryForm) throws Exception {
        LocalDate currentDate = LocalDate.now();
        int year = currentDate.getYear();
        String yearStr = Integer.toString(year);
        String years = yearStr.substring(yearStr.length() - 2);
        List<ProductChuciPeriod> list = productChuciPeriodMapper.getChuciProductCode(queryForm);
        for (ProductChuciPeriod productChuciPeriod : list) {
            String factoryName = productChuciPeriod.getFactoryName().split("-")[0];
            String productCode = factoryName + years + productChuciPeriod.getProductCode();
            productChuciPeriod.setProductCode(productCode);
        }

        return list;
    }

    @Override
    public List<LineTask> getNameByNumberList(QueryForm queryForm) throws Exception {
        LineTaskExample taskExample = new LineTaskExample();
        taskExample.createCriteria().andFactoryNameEqualTo(queryForm.getFactoryName()).andTaskNameEqualTo(queryForm.getTaskName());
        List<LineTask> lineTasks = lineTaskMapper.selectByExample(taskExample);
        return lineTasks;
    }

    @Override
    public Map<String, Object> getLineTaskListByApp(QueryForm queryForm) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
//    String factoryName=queryForm.getFactoryName();
        //生产线边任务---批次任务
        if (queryForm.getType().equals("1")) {
            //批次任务待处理
            queryForm.setLineType("1");
            queryForm.setLineStatus("0");
            List<LineTask> toBeCompletedList = lineTaskMapper.getLineTaskListByCompleted(queryForm);
            setFileInfosForLineTasks(toBeCompletedList);
            if (toBeCompletedList.size() > 0) {
                toBeCompletedList.get(0).setCount(toBeCompletedList.size());
                resultMap.put("toBeCompletedList", toBeCompletedList);
            } else {
                resultMap.put("toBeCompletedList", new ArrayList<>());
            }
            //批次任务待确认
            queryForm.setLineType("1");
            queryForm.setLineStatus("1");
            List<LineTask> CompletedList = lineTaskMapper.getLineTaskListByCompleted(queryForm);
            setFileInfosForLineTasks(CompletedList);
            if (CompletedList.size() > 0) {
                CompletedList.get(0).setCount(CompletedList.size());
                resultMap.put("CompletedList", CompletedList);
            } else {
                resultMap.put("CompletedList", new ArrayList<>());
            }
            //批次任务已完成
            queryForm.setLineType("1");
            queryForm.setLineStatus("2");
            List<LineTask> isCompletedList = lineTaskMapper.getLineTaskListByCompleted(queryForm);
            setFileInfosForLineTasks(isCompletedList);
            if (isCompletedList.size() > 0) {
                isCompletedList.get(0).setCount(isCompletedList.size());
                resultMap.put("isCompletedList", isCompletedList);
            } else {
                resultMap.put("isCompletedList", new ArrayList<>());
            }
            //生产线边任务---本班
        } else if (queryForm.getType().equals("2")) {
            //本班任务待处理
            LineTaskExample taskExample = new LineTaskExample();
            LineTaskExample.Criteria criteria = taskExample.createCriteria();
            criteria.andLineTypeEqualTo("2");
            criteria.andLineStatusEqualTo("0");
            criteria.andFactoryNameEqualTo(queryForm.getFactoryName());
            if (StringUtils.isNotBlank(queryForm.getProductCode())) {
                criteria.andProductCodeEqualTo(queryForm.getProductCode());
            }
            if (StringUtils.isNotBlank(queryForm.getWorkProcess())) {
                criteria.andWorkProcessEqualTo(queryForm.getWorkProcess());
            }
            if (StringUtils.isNotBlank(queryForm.getTaskName())) {
                criteria.andTaskNameEqualTo(queryForm.getTaskName());
            }
//        taskExample.createCriteria().andLineTypeEqualTo("2").andLineStatusEqualTo("0").andFactoryNameEqualTo(queryForm.getFactoryName());
            List<LineTask> toBeCompletedList = lineTaskMapper.selectByExample(taskExample);
            setFileInfosForLineTasks(toBeCompletedList);
            if (toBeCompletedList.size() > 0) {
                toBeCompletedList.get(0).setCount(toBeCompletedList.size());
                resultMap.put("toBeCompletedList", toBeCompletedList);
            } else {
                resultMap.put("toBeCompletedList", new ArrayList<>());
            }

            //本班任务待确认
            LineTaskExample taskExample1 = new LineTaskExample();
            LineTaskExample.Criteria criteria1 = taskExample1.createCriteria();
            criteria1.andLineTypeEqualTo("2");
            criteria1.andLineStatusEqualTo("1");
            criteria1.andFactoryNameEqualTo(queryForm.getFactoryName());
            if (StringUtils.isNotBlank(queryForm.getProductCode())) {
                criteria1.andProductCodeEqualTo(queryForm.getProductCode());
            }
            if (StringUtils.isNotBlank(queryForm.getWorkProcess())) {
                criteria1.andWorkProcessEqualTo(queryForm.getWorkProcess());
            }
            if (StringUtils.isNotBlank(queryForm.getTaskName())) {
                criteria1.andTaskNameEqualTo(queryForm.getTaskName());
            }
//        taskExample1.createCriteria().andLineTypeEqualTo("2").andLineStatusEqualTo("1").andFactoryNameEqualTo(queryForm.getFactoryName());
            List<LineTask> CompletedList = lineTaskMapper.selectByExample(taskExample1);
            setFileInfosForLineTasks(CompletedList);
            if (CompletedList.size() > 0) {
                CompletedList.get(0).setCount(CompletedList.size());
                resultMap.put("CompletedList", CompletedList);
            } else {
                resultMap.put("CompletedList", new ArrayList<>());
            }

            //本班任务已完成

            LineTaskExample taskExample2 = new LineTaskExample();
            LineTaskExample.Criteria criteria2 = taskExample2.createCriteria();
            criteria2.andLineTypeEqualTo("2");
            criteria2.andLineStatusEqualTo("2");
            criteria2.andFactoryNameEqualTo(queryForm.getFactoryName());
            if (StringUtils.isNotBlank(queryForm.getProductCode())) {
                criteria2.andProductCodeEqualTo(queryForm.getProductCode());
            }
            if (StringUtils.isNotBlank(queryForm.getWorkProcess())) {
                criteria2.andWorkProcessEqualTo(queryForm.getWorkProcess());
            }
            if (StringUtils.isNotBlank(queryForm.getTaskName())) {
                criteria2.andTaskNameEqualTo(queryForm.getTaskName());
            }
//        taskExample2.createCriteria().andLineTypeEqualTo("2").andLineStatusEqualTo("2").andFactoryNameEqualTo(queryForm.getFactoryName());
            List<LineTask> isCompletedList = lineTaskMapper.selectByExample(taskExample2);
            setFileInfosForLineTasks(isCompletedList);
            if (isCompletedList.size() > 0) {
                isCompletedList.get(0).setCount(isCompletedList.size());
                resultMap.put("isCompletedList", isCompletedList);
            } else {
                resultMap.put("isCompletedList", new ArrayList<>());
            }
            //生产线边任务---今日或其他
        } else if (queryForm.getType().equals("3")) {
            ArrayList<String> list = new ArrayList<>();
            list.add("3");
            list.add("4");
            queryForm.setTypeList(list);
            queryForm.setLineStatus("0");
            List<LineTask> toBeCompletedList = lineTaskMapper.seletByother(queryForm);
            setFileInfosForLineTasks(toBeCompletedList);
            if (toBeCompletedList.size() > 0) {
                toBeCompletedList.get(0).setCount(toBeCompletedList.size());
                resultMap.put("toBeCompletedList", toBeCompletedList);
            } else {
                resultMap.put("toBeCompletedList", new ArrayList<>());
            }

            //本班任务待确认
            queryForm.setLineStatus("1");
            List<LineTask> CompletedList = lineTaskMapper.seletByother(queryForm);
            setFileInfosForLineTasks(CompletedList);
            if (CompletedList.size() > 0) {
                CompletedList.get(0).setCount(CompletedList.size());
                resultMap.put("CompletedList", CompletedList);
            } else {
                resultMap.put("CompletedList", new ArrayList<>());
            }

            //本班任务已完成
            queryForm.setLineStatus("2");
            List<LineTask> isCompletedList = lineTaskMapper.seletByother(queryForm);
            setFileInfosForLineTasks(isCompletedList);
            if (isCompletedList.size() > 0) {
                isCompletedList.get(0).setCount(isCompletedList.size());
                resultMap.put("isCompletedList", isCompletedList);
            } else {
                resultMap.put("isCompletedList", new ArrayList<>());
            }

            //生产待确认
        } else if (queryForm.getType().equals("4")) {
            //生产待确认待完成
            List<LineTask> CompletedList = lineTaskMapper.getProductionCompleted(queryForm);
            setFileInfosForLineTasks(CompletedList);
            if (CompletedList.size() > 0) {
                CompletedList.get(0).setCount(CompletedList.size());
                resultMap.put("CompletedList", CompletedList);
            } else {
                resultMap.put("CompletedList", new ArrayList<>());
            }
            List<LineTask> isCompletedList = lineTaskMapper.getProductionIsCompleted(queryForm);
            setFileInfosForLineTasks(isCompletedList);
            if (isCompletedList.size() > 0) {
                isCompletedList.get(0).setCount(isCompletedList.size());
                resultMap.put("isCompletedList", isCompletedList);
            } else {
                resultMap.put("isCompletedList", new ArrayList<>());
            }
            //品控待确认
        } else if (queryForm.getType().equals("5")) {
            //品控待确认待完成
            List<LineTask> CompletedList = lineTaskMapper.getProductionCompletedByPk(queryForm);

            List<LineTask> filteredList = CompletedList.stream()
                    .filter(lineTask -> !lineTask.getTaskName().equals("包装机维保"))
                    .collect(Collectors.toList());
            setFileInfosForLineTasks(filteredList);
            //判断包装机维保不需要进入品控确认
            if (filteredList.size() > 0) {
                filteredList.get(0).setCount(filteredList.size());
                resultMap.put("CompletedList", filteredList);
            } else {
                resultMap.put("CompletedList", new ArrayList<>());
            }
            //品控待确认已完成
            LineTaskExample taskExample3 = new LineTaskExample();
            LineTaskExample.Criteria criteria3 = taskExample3.createCriteria();
            criteria3.andLineStatusEqualTo("2");
            criteria3.andFactoryNameEqualTo(queryForm.getFactoryName());
            if (StringUtils.isNotBlank(queryForm.getProductCode())) {
                criteria3.andProductCodeEqualTo(queryForm.getProductCode());
            }
            if (StringUtils.isNotBlank(queryForm.getWorkProcess())) {
                criteria3.andWorkProcessEqualTo(queryForm.getWorkProcess());
            }
            if (StringUtils.isNotBlank(queryForm.getTaskName())) {
                criteria3.andTaskNameEqualTo(queryForm.getTaskName());
            }
            List<LineTask> isCompletedList = lineTaskMapper.selectByExample(taskExample3);

            List<LineTask> filteredList1 = isCompletedList.stream()
                    .filter(lineTask -> !lineTask.getTaskName().equals("包装机维保"))
                    .collect(Collectors.toList());
            setFileInfosForLineTasks(filteredList1);
            //判断包装机维保不需要进入品控确认
            if (filteredList1.size() > 0) {
                filteredList1.get(0).setCount(filteredList1.size());
                resultMap.put("isCompletedList", filteredList1);
            } else {
                resultMap.put("isCompletedList", new ArrayList<>());
            }
        }
        return resultMap;
    }

    private void setFileInfosForLineTasks(List<LineTask> lineTasks) {
        for (LineTask lineTask : lineTasks) {
            if (StringUtils.isNotBlank(lineTask.getPhotoId())) {
                String[] split = lineTask.getPhotoId().split(",");
                List<String> fileUrls = new ArrayList<>();
                for (String s : split) {
                    FileInfoExample example = new FileInfoExample();
                    example.createCriteria().andIdEqualTo(s);
                    List<FileInfo> fileInfos = fileInfoMapper.selectByExample(example);
                    if (!fileInfos.isEmpty()) {
                        fileUrls.add(fileInfos.get(0).getFileUrl());
                    }
                }
                lineTask.setFileInfos(fileUrls);
            }
        }
    }

    @Override
    public String addApplication(LineTask lineTask) throws Exception {

        if (null == lineTask) {
            return "请输入生产线边任务";
        }
        String result = this.lineTaskAddChange(lineTask);
        if (!"".equals(result)) {
            return result;
        }
        String id = CommonUtil.generateRandomNum("lineTask_");
        if (lineTask.getTaskName().equals("清洗罐清水流转")) {
            lineTask.setLineStatus("2");
        } else {
            lineTask.setLineStatus("1");
        }
        lineTask.setId(id);
        lineTask.setStatus("0");
        lineTask.setLineType("5");

        lineTask.setCreateTime(new Date());
        int count = lineTaskMapper.insertSelective(lineTask);

        if (count < 1) {
            return "新增失败！";
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateLineTask(LineTask lineTask) throws Exception {
        String id = lineTask.getId();
        if (StringUtils.isEmpty(id)) {
            return "id is null";
        }
        String result = this.lineTaskUpdateChange(lineTask);
        if (!"".equals(result)) {
            return result;
        }
        LineTaskExample example = new LineTaskExample();
        example.createCriteria().andIdEqualTo(id);
        lineTask.setUpdateTime(new Date());
        int count = lineTaskMapper.updateByExampleSelective(lineTask, example);
        return CommonUtil.outStr(count);
    }

    @Scheduled(cron = "*/59 * * * * ?")
    public void updateScheduledLineTask() throws Exception {
//        List<ProductThickPeriod> productThickPeriods = productThickPeriodService.getLineTask();
        List<LineTask> lineTasks = Lists.newArrayList();
//        if (CollectionsUtil.isNotEmpty(productThickPeriods)) {
//            List<String> thickIds = Lists.newArrayList();
//            for (ProductThickPeriod thickPeriod : productThickPeriods) {
//                LineTask lineTask = new LineTask();
//                lineTask.setAttr12(thickPeriod.getId());
//                lineTask.setFactoryName(thickPeriod.getFactoryName());
//                lineTask.setLineType("1");
//                lineTask.setWorkPeriod("混料");
//                lineTask.setWorkProcess("细磨");
//                lineTask.setProductCode(thickPeriod.getStartCode());
//                String banCi = StringUtils.equals(thickPeriod.getBanCi(), "1") ? "A" : "B";
//                lineTask.setBanCi(banCi);
//                lineTask.setTaskName("砂磨机清洗");
//                lineTask.setFrequency("1次/批");
//                lineTask.setId(CommonUtil.generateRandomNum("linetask-"));
//                lineTasks.add(lineTask);
//                thickIds.add(thickPeriod.getId());
//            }
//            String resp = productThickPeriodService.updateThickPeriodByIds("1",thickIds);
//            if (StringUtils.isNotEmpty(resp)) {
//                return;
//            }
//        }
        List<ProductThickPeriod> productThickPeriods = productThickPeriodService.getXunTask();
        if (CollectionsUtil.isNotEmpty(productThickPeriods)) {
            List<String> thickIds = Lists.newArrayList();
            for (ProductThickPeriod thickPeriod : productThickPeriods) {
                LineTask lineTask = new LineTask();
                lineTask.setAttr12(thickPeriod.getId());
                lineTask.setFactoryName(thickPeriod.getFactoryName());
                lineTask.setLineType("1");
                lineTask.setWorkPeriod("混料");
                lineTask.setWorkProcess("细磨");
                lineTask.setProductCode(thickPeriod.getStartCode());
                String banCi = StringUtils.equals(thickPeriod.getBanCi(), "1") ? "A" : "B";
                lineTask.setBanCi(banCi);
                lineTask.setTaskName("砂磨机料仓压力");
                lineTask.setFrequency("1次/批");
                lineTask.setId(CommonUtil.generateRandomNum("linetask-"));
                lineTasks.add(lineTask);
                thickIds.add(thickPeriod.getId());
            }
            String resp = productThickPeriodService.updateThickPeriodByIds("2", thickIds);
            if (StringUtils.isNotEmpty(resp)) {
                return;
            }
        }

        List<LineTask> lineTasks1 = lineTaskMapper.selectLineTasksByType();
        Map<String, LineTask> latestLineTaskMap = Maps.newHashMap();
        for (LineTask task : lineTasks1) {
            String factoryName = task.getFactoryName();
            String taskName = task.getTaskName();
            if (latestLineTaskMap.containsKey(factoryName + "-" + taskName)) {
                continue;
            }

            latestLineTaskMap.put(factoryName + "-" + taskName, task);
        }

        Calendar calendar = Calendar.getInstance();
        int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
        if ((hourOfDay >= 9 && hourOfDay < 19) || (hourOfDay >= 21)) {
            String productCode1 = productChuciPeriodService.getLatestProductCode("16-1");
            String productCode2 = productChuciPeriodService.getLatestProductCode("16-2");
            this.addLineBanciTask("16-1", "混料除磁器清洗", "混料", "除磁", productCode1, lineTasks, latestLineTaskMap);
            this.addLineBanciTask("16-2", "混料除磁器清洗", "混料", "除磁", productCode2, lineTasks, latestLineTaskMap);
        }
//        this.addLineBanciTask("16-1", "过滤器筛网检查", "", "干燥", "", lineTasks, latestLineTaskMap);
//        this.addLineBanciTask("16-2", "过滤器筛网检查", "", "干燥", "", lineTasks, latestLineTaskMap);


//        this.addLineBanciTask("16-1", "振筛清理", "", "混料", "", lineTasks, latestLineTaskMap);
//        this.addLineBanciTask("16-2", "振筛清理", "", "混料", "", lineTasks, latestLineTaskMap);
        //干燥筛网检查
        this.addLineBanciTask("16-1", "干燥筛网检查", "", "干燥", "", lineTasks, latestLineTaskMap);
        this.addLineBanciTask("16-2", "干燥筛网检查", "", "干燥", "", lineTasks, latestLineTaskMap);

//        this.addLineMothTask("16-1", "振筛清理", lineTasks, latestLineTaskMap);
//        this.addLineMothTask("16-2", "振筛清理", lineTasks, latestLineTaskMap);

//        this.addLineWeekTask("16-1", "干燥送风机清理", lineTasks, latestLineTaskMap);
//        this.addLineWeekTask("16-2", "干燥送风机清理", lineTasks, latestLineTaskMap);

//        this.addLineWeekTask("16-1", "干燥U型管检查", lineTasks, latestLineTaskMap);
//        this.addLineWeekTask("16-2", "干燥U型管检查", lineTasks, latestLineTaskMap);

        //添加粗磨记录
        List<ProductThinPeriod> productThinPeriods = productThinPeriodService.getProductThinPeriodByLineStatus();
        if (CollectionsUtil.isNotEmpty(productThinPeriods)) {
            List<String> thinIds = Lists.newArrayList();
            for (ProductThinPeriod thinPeriod : productThinPeriods) {
                LineTask lineTask = new LineTask();
                lineTask.setAttr12(thinPeriod.getId());
                lineTask.setFactoryName(thinPeriod.getFactoryName());
                lineTask.setLineType("1");
                lineTask.setWorkPeriod("混料");
                lineTask.setWorkProcess("粗磨");
                lineTask.setProductCode(thinPeriod.getStartCode());
                String banCi = StringUtils.equals(thinPeriod.getBanCi(), "1") ? "A" : "B";
                lineTask.setBanCi(banCi);
                lineTask.setTaskName("粗磨记录");
                lineTask.setFrequency("1次/批");
                //开始时间往后的字段依次是attr1-
                //开始时间
                lineTask.setAttr1(thinPeriod.getStartTime());
                //结束时间
                lineTask.setAttr2(thinPeriod.getEndTime());
                //转速
                lineTask.setAttr3(thinPeriod.getRevoSpeed());
                //attr4是投料工位
                lineTask.setId(CommonUtil.generateRandomNum("linetask-"));
                lineTasks.add(lineTask);
                thinIds.add(thinPeriod.getId());
            }
            String resp = productThinPeriodService.updateProductThinPeriodLineTaskByIds(thinIds, "1");
            if (StringUtils.isNotEmpty(resp)) {
                throw new Exception(resp);
            }
        }

        //细磨中转数据
        List<ProductThickPeriodTime> productThickPeriodTimes = productThickPeriodService.getProductThickTimeByStatus();
        if (CollectionsUtil.isNotEmpty(productThickPeriodTimes)) {
            for (ProductThickPeriodTime periodTime : productThickPeriodTimes) {
                LineTask lineTask = new LineTask();
                lineTask.setAttr12(periodTime.getThickId());
                lineTask.setAttr11(periodTime.getNumberTime());
                lineTask.setFactoryName(periodTime.getFactoryName());
                lineTask.setLineType("1");
                lineTask.setWorkPeriod("混料");
                lineTask.setWorkProcess("细磨");
                lineTask.setProductCode(periodTime.getProductCode());
                String banCi = StringUtils.equals(periodTime.getBanCi(), "1") ? "A" : "B";
                lineTask.setBanCi(banCi);
                lineTask.setTaskName("细磨记录");
                lineTask.setFrequency("1次/批");
                //开始时间往后的字段依次是attr1-
                //开始时间
                lineTask.setAttr1(periodTime.getStartTime());
                //结束时间
                lineTask.setAttr2(periodTime.getEndTime());
                //转速
                lineTask.setAttr4(periodTime.getRevoSpeed());
                //料温
                lineTask.setAttr5(periodTime.getTemperature());
                lineTask.setId(CommonUtil.generateRandomNum("linetask-"));
                lineTasks.add(lineTask);
            }
            String resp = productThickPeriodService.updateThickPeriodLineStatus(productThickPeriodTimes, "1");
            if (StringUtils.isNotEmpty(resp)) {
                throw new Exception(resp);
            }
        }

        if (CollectionsUtil.isNotEmpty(lineTasks)) {
            int count = lineTaskMapper.batchInsertLineTasks(lineTasks);
            if (count < 0) {
                throw new Exception("插入线检表失败");
            }
        }

    }

    @Scheduled(cron = "*/40 * * * * ?")
    public void updateProductData() throws Exception {
        List<LineTask> lineTasks = lineTaskMapper.selectCuMoRecordLineTasks();
        List<String> thinIds = Lists.newArrayList();
        if (CollectionsUtil.isNotEmpty(lineTasks)) {
            for (LineTask task : lineTasks) {
                if (StringUtils.isEmpty(task.getAttr12())) {
                    continue;
                }
                thinIds.add(task.getAttr12());
            }
        }

        List<LineTask> updates = Lists.newArrayList();
        if (CollectionsUtil.isNotEmpty(thinIds)) {
            List<ProductThinPeriod> thinPeriods = productThinPeriodService.getProductThinPeriodListByIds(thinIds);
            Map<String, ProductThinPeriod> productThinPeriodMap = Maps.newHashMap();
            for (ProductThinPeriod thinPeriod : thinPeriods) {
                productThinPeriodMap.put(thinPeriod.getId(), thinPeriod);
            }

            for (LineTask task : lineTasks) {
                if (StringUtils.isEmpty(task.getAttr12())) {
                    continue;
                }
                String attr12 = task.getAttr12();
                if (productThinPeriodMap.containsKey(attr12)) {
                    ProductThinPeriod exist = productThinPeriodMap.get(attr12);
                    if (StringUtils.isNotEmpty(exist.getRevoSpeed()) || StringUtils.isNotEmpty(exist.getEndTime())) {
                        LineTask update = new LineTask();
                        update.setId(task.getId());
                        update.setAttr1(exist.getStartTime());
                        //结束时间
                        update.setAttr2(exist.getEndTime());
                        //转速
                        update.setAttr3(exist.getRevoSpeed());
                        updates.add(update);
                    }
                }
            }
        }

        List<LineTask> lineXimoTasks = lineTaskMapper.selectXiMoRecordLineTasks();
        List<ProductThickPeriodTime> thickPeriodTimes = Lists.newArrayList();
        if (CollectionsUtil.isNotEmpty(lineXimoTasks)) {
            for (LineTask task : lineXimoTasks) {
                ProductThickPeriodTime periodTime = new ProductThickPeriodTime();
                periodTime.setThickId(task.getAttr12());
                periodTime.setNumberTime(task.getAttr11());
                thickPeriodTimes.add(periodTime);
            }
            List<ProductThickPeriodTime> existDatas = productThickPeriodService.getUnDoneProductThickLineTaskByIds(thickPeriodTimes);
            Map<String, ProductThickPeriodTime> productThickPeriodMap = Maps.newHashMap();
            for (ProductThickPeriodTime thick : existDatas) {
                productThickPeriodMap.put(thick.getThickId() + "-" + thick.getNumberTime(), thick);
            }
            for (LineTask task : lineXimoTasks) {
                if (StringUtils.isEmpty(task.getAttr12()) || StringUtils.isEmpty(task.getAttr11())) {
                    continue;
                }
                String attr12 = task.getAttr12();
                String attr11 = task.getAttr11();
                String key = attr12 + "-" + attr11;
                if (productThickPeriodMap.containsKey(key)) {
                    ProductThickPeriodTime exist = productThickPeriodMap.get(key);
                    if (StringUtils.isNotEmpty(exist.getRevoSpeed()) || StringUtils.isNotEmpty(exist.getEndTime()) || StringUtils.isNotEmpty(exist.getTemperature())) {
                        LineTask update = new LineTask();
                        update.setId(task.getId());
                        update.setAttr1(exist.getStartTime());
                        update.setAttr2(exist.getEndTime());
                        //转速
                        update.setAttr4(exist.getRevoSpeed());
                        //料温
                        update.setAttr5(exist.getTemperature());
                        updates.add(update);
                    }
                }
            }
        }

        List<LineTask> lineGzTasks = lineTaskMapper.selectGzRecordLineTasks();
        List<String> productGzPeriodIds = Lists.newArrayList();
        if (CollectionsUtil.isNotEmpty(lineGzTasks)) {
            for (LineTask task : lineGzTasks) {
                productGzPeriodIds.add(task.getAttr13());
            }
            List<ProductGzPeriod> existDatas = productGzPeriodService.getProductGzPeriodByIds(productGzPeriodIds);
            Map<String, ProductGzPeriod> productGzPeriodMap = Maps.newHashMap();
            for (ProductGzPeriod gz : existDatas) {
                productGzPeriodMap.put(gz.getId(), gz);
            }
            for (LineTask task : lineGzTasks) {
                if (StringUtils.isEmpty(task.getAttr13())) {
                    continue;
                }

                if (productGzPeriodMap.containsKey(task.getAttr13())) {
                    ProductGzPeriod exist = productGzPeriodMap.get(task.getAttr13());
                    LineTask update = new LineTask();
                    update.setId(task.getId());
                    //开始时间
                    update.setAttr2(exist.getStartTime());
                    //结束时间
                    update.setAttr3(exist.getEndTime());
                    //消耗时间
                    update.setAttr4(exist.getSpendTime());
                    //共料bang压力
                    update.setAttr7(exist.getWuHuaPa());
                    //总进料流量
                    update.setAttr8(exist.getLiuLiang());
                    //雾化频率转速
                    update.setAttr9(exist.getZhuanSu());
                    //进风温度
                    update.setAttr10(exist.getJinFenTemp());
                    //排风温度
                    update.setAttr11(exist.getPaiFenTemp());
                    //油温
                    update.setAttr12(exist.getWuHuaTemp());

                    updates.add(update);
                }
            }
        }
        if (CollectionsUtil.isNotEmpty(updates)) {
            lineTaskMapper.batchUpdateLineTasks(updates);
        }
    }

    private void addLineBanciTask(String factoryName, String taskName, String workPeriod, String process, String code, List<LineTask> lineTasks, Map<String, LineTask> latestLineTaskMap) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(new Date());
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String banci = StringUtils.equals(dutyMap.get("whiteDay"), "1") ? "A" : "B";
        boolean szql = false;
        if (latestLineTaskMap.containsKey(factoryName + "-" + taskName)) {
            LineTask task = latestLineTaskMap.get(factoryName + "-" + taskName);
            String createTime = simpleDateFormat.format(task.getCreateTime());
            if (!StringUtils.equals(today, createTime) || !StringUtils.equals(task.getBanCi(), banci)) {
                szql = true;
            }
        } else {
            szql = true;
        }
        if (szql) {
            if (StringUtils.equals("混料除磁器清洗", taskName)) {
                for (int i = 1; i <= 3; i++) {
                    LineTask lineTask = new LineTask();
                    lineTask.setFactoryName(factoryName);
                    lineTask.setLineType("2");
                    lineTask.setWorkPeriod(workPeriod);
                    lineTask.setWorkProcess(process);
                    lineTask.setProductCode(code);
                    lineTask.setBanCi(banci);
                    lineTask.setTaskName(taskName);
                    lineTask.setAttr1(i + "");
                    lineTask.setFrequency("1次/班");
                    lineTask.setId(CommonUtil.generateRandomNum("linetask-"));
                    lineTasks.add(lineTask);
                }
            } else if (StringUtils.equals("干燥筛网检查", taskName)) {
                for (int i = 1; i <= 12; i++) {
                    LineTask lineTask = new LineTask();
                    lineTask.setFactoryName(factoryName);
                    lineTask.setLineType("2");
                    lineTask.setWorkPeriod(workPeriod);
                    lineTask.setWorkProcess(process);
                    lineTask.setProductCode(code);
                    lineTask.setBanCi(banci);
                    lineTask.setTaskName(taskName);
                    lineTask.setAttr1(i + "");
                    lineTask.setFrequency("1次/班");
                    lineTask.setId(CommonUtil.generateRandomNum("linetask-"));
                    lineTasks.add(lineTask);
                }
            } else {
                LineTask lineTask = new LineTask();
                lineTask.setFactoryName(factoryName);
                lineTask.setLineType("2");
                lineTask.setWorkPeriod(workPeriod);
                lineTask.setWorkProcess(process);
                lineTask.setProductCode(code);
                lineTask.setBanCi(banci);
                lineTask.setTaskName(taskName);
                lineTask.setFrequency("1次/班");
                lineTask.setId(CommonUtil.generateRandomNum("linetask-"));
                lineTasks.add(lineTask);
            }
        }
    }

    private void addLineWeekTask(String factoryName, String taskName, List<LineTask> lineTasks, Map<String, LineTask> latestLineTaskMap) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String onlineTime = config.onlineTime;
        Date now = new Date();
        long distDay = (now.getTime() - simpleDateFormat.parse(onlineTime).getTime()) / (1000 * 60 * 60 * 24);
        boolean szql = false;
        if (latestLineTaskMap.containsKey(factoryName + "-" + taskName)) {
            LineTask task = latestLineTaskMap.get(factoryName + "-" + taskName);
            if ((now.getTime() - task.getCreateTime().getTime()) / (1000 * 60 * 60 * 24) >= 7) {
                szql = true;
            }
        } else if (distDay >= 7) {
            szql = true;
        }
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String banci = StringUtils.equals(dutyMap.get("whiteDay"), "1") ? "A" : "B";
        if (szql) {
            LineTask addLineTask = new LineTask();
            addLineTask.setFactoryName(factoryName);
            addLineTask.setLineType("3");
            addLineTask.setWorkProcess("干燥");
            addLineTask.setBanCi(banci);
            addLineTask.setTaskName(taskName);
            addLineTask.setFrequency("1次/周");
            addLineTask.setId(CommonUtil.generateRandomNum("linetask-"));
            lineTasks.add(addLineTask);
        }
    }

    private void addLineMothTask(String factoryName, String taskName, List<LineTask> lineTasks, Map<String, LineTask> latestLineTask) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String onlineTime = config.onlineTime;
        Date now = new Date();
        long distDay = (now.getTime() - simpleDateFormat.parse(onlineTime).getTime()) / (1000 * 60 * 60 * 24);
        boolean szql = false;
        if (latestLineTask.containsKey(factoryName + "-" + taskName)) {
            LineTask task = latestLineTask.get(factoryName + "-" + taskName);
            if ((now.getTime() - task.getCreateTime().getTime()) / (1000 * 60 * 60 * 24) >= 15) {
                szql = true;
            }
        } else if (distDay >= 15) {
            szql = true;
        }
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String banci = StringUtils.equals(dutyMap.get("whiteDay"), "1") ? "A" : "B";
        if (szql) {
            LineTask addLineTask = new LineTask();
            addLineTask.setFactoryName(factoryName);
            addLineTask.setLineType("4");
            addLineTask.setWorkProcess("干燥");
            addLineTask.setBanCi(banci);
            addLineTask.setTaskName(taskName);
            addLineTask.setFrequency("1次/半月");
            addLineTask.setId(CommonUtil.generateRandomNum("linetask-"));
            lineTasks.add(addLineTask);
        }
    }

    public String getNameStr(String type) {
        if (type.equals("0")) {
            return "新滤网上线首检";
        } else if (type.equals("1")) {
            return "投料除磁设备清理";
        } else if (type.equals("2")) {
            return "混料除磁器清洗";
        } else if (type.equals("3")) {
            return "混料振筛检查清理";
        } else if (type.equals("4")) {
            return "混料筛网检查";
        } else if (type.equals("5")) {
            return "粗磨记录";
        } else if (type.equals("6")) {
            return "细磨记录";
        } else if (type.equals("7")) {
            return "砂磨机清洗";
        } else if (type.equals("8")) {
            return "小推车和磁棒使用";
        } else if (type.equals("9")) {
            return "清洗罐清水流转";
        } else if (type.equals("10")) {
            return "砂磨机料仓压力";
        } else if (type.equals("11")) {
            return "送风机清理";
        } else if (type.equals("12")) {
            return "U型管检查";
        } else if (type.equals("13")) {
            return "干燥筛网检查";
        } else if (type.equals("14")) {
            return "干燥送风机清理";
        } else if (type.equals("15")) {
            return "干燥U型管检查";
        } else if (type.equals("16")) {
            return "干燥工序记录";
        } else if (type.equals("17")) {
            return "过滤器筛网检査";
        } else if (type.equals("18")) {
            return "包装机维保";
        }
        return "";
    }


    public ArrayList<String> getTypeStr(String type) {
        ArrayList<String> list = new ArrayList<>();
        if (type.equals("0")) {
            list.add("1");
            list.add("2");
        } else if (type.equals("1")) {
            list.add("0");
            list.add("1");
            list.add("2");
        } else if (type.equals("2")) {
            list.add("1");
            list.add("2");
        } else if (type.equals("3")) {
            list.add("0");
            list.add("1");
            list.add("2");
        } else if (type.equals("4")) {
            list.add("0");
            list.add("1");
            list.add("2");
        } else if (type.equals("5")) {
            list.add("0");
            list.add("1");
            list.add("2");
        } else if (type.equals("6")) {
            list.add("0");
            list.add("1");
            list.add("2");
        } else if (type.equals("7")) {
            list.add("0");
            list.add("1");
            list.add("2");
        } else if (type.equals("8")) {
            list.add("1");
            list.add("2");
        } else if (type.equals("9")) {
            list.add("0");
            list.add("1");
            list.add("2");
        }
        return list;
    }

    //快捷应用新增必填项
    private String lineTaskAddChange(LineTask lineTask) {

        String result = "";

        if (lineTask.getTaskName().equals("新滤网上线首检")) {
            if (StringUtils.isBlank(lineTask.getFactoryName())) {
                result = "请输入厂房车间";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr1())) {
                result = "请输入过滤器位置";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr2())) {
                result = "请输入滤网完整度";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr3())) {
                result = "请输入滤网清洁度";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr4())) {
                result = "请输入铭牌/钢印目数";
                return result;
            }
        } else if (lineTask.getTaskName().equals("投料除磁设备清理")) {
            if (StringUtils.isBlank(lineTask.getFactoryName())) {
                result = "请输入厂房车间";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr1())) {
                result = "请输入投料站号";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr2())) {
                result = "请输入拆机人";
                return result;
            }
        } else if (lineTask.getTaskName().equals("包装机维保")) {
            if (StringUtils.isBlank(lineTask.getFactoryName())) {
                result = "请输入厂房车间";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr1())) {
                result = "请输入投料站号";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr2())) {
                result = "请输入故障描述";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr3())) {
                result = "请输入拆机部位";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr4())) {
                result = "请输入卸料去向人";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr5())) {
                result = "请输入成品批次";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr6())) {
                result = "请输入成品包数";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr7())) {
                result = "请输入维修开始";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr6())) {
                result = "请输入维修结束";
                return result;
            }
        }
        return result;


    }

    //修改必填项
    private String lineTaskUpdateChange(LineTask lineTask) {

        String result = "";

        if (lineTask.getTaskName().equals("混料除磁器清洗")) {
            if (StringUtils.isBlank(lineTask.getAttr1())) {
                result = "请输入清洗情况";
                return result;
            }
        } else if (lineTask.getTaskName().equals("砂磨机清洗")) {
            if (StringUtils.isBlank(lineTask.getAttr1())) {
                result = "请选择砂磨机号";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr2())) {
                result = "请输入清洗类别";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr3())) {
                result = "请输入锆球重量";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr4())) {
                result = "请输入锆球形态";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr5())) {
                result = "请输入锆球直径";
                return result;
            }
        } else if (lineTask.getTaskName().equals("过滤器筛网检查")) {
            if (StringUtils.isBlank(lineTask.getAttr1())) {
                result = "请输入筛网编号";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr2())) {
                result = "请输入筛网目数";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr3())) {
                result = "请输入杂质过筛有无锆球";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr4())) {
                result = "请输入筛网是否破损";
                return result;
            }
        } else if (lineTask.getTaskName().equals("干燥送风机清理")) {
            if (StringUtils.isBlank(lineTask.getAttr1())) {
                result = "请输入送风机号";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr2())) {
                result = "请输入滤网清理";
                return result;
            }
        } else if (lineTask.getTaskName().equals("干燥U型管检查")) {
            if (StringUtils.isBlank(lineTask.getAttr1())) {
                result = "请输入清洗机号";
                return result;
            }
            if (StringUtils.isBlank(lineTask.getAttr2())) {
                result = "请输入清理物料重量";
                return result;
            }
        }
        return result;


    }


}
